﻿/*-----------------------------------------------
// File: MsgCtrl.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Battle.Communication
{
    public delegate void PushMessage(BattlePacket bp);
    public class MsgCtrl {

        Queue<BattlePacket> ViewToEngineMsg;
        Queue<BattlePacket> EngineToViewMsg;

        PushMessage viewToEngineHandler;
        PushMessage engineToViewHandler;

        public MsgCtrl() {
            Reset();
        }

        public void Reset() {
            ViewToEngineMsg = new Queue<BattlePacket>();
            EngineToViewMsg = new Queue<BattlePacket>();
            viewToEngineHandler = null;
            engineToViewHandler = null;
        }

        public void ViewSend(BattlePacket bp) {
            ViewToEngineMsg.Enqueue(bp);
        }
        public void EngineSend(BattlePacket bp) {
            EngineToViewMsg.Enqueue(bp);
        }

        public void Update() {
            while (ViewToEngineMsg.Count > 0) {
                var bp = ViewToEngineMsg.Dequeue();
                if (viewToEngineHandler != null) {
                    viewToEngineHandler(bp);
                }
            }

            while (EngineToViewMsg.Count > 0) {
                var bp = EngineToViewMsg.Dequeue();
                if (engineToViewHandler != null) {
                    engineToViewHandler(bp);
                }
            }
        }

        public void RegistViewToEngineHandler(PushMessage handler) {
            viewToEngineHandler = handler;
        }

        public void RegistEngineToViewHandler(PushMessage handler) {
            engineToViewHandler = handler;
        }



    }
}